var CAW_UI = namespace("caw.ui");
var CAW_CLIENT = namespace("caw.ws.client");

CAW_UI.showMessage = function(selector, type, jsonData) {

	$(selector).dialog({
		autoOpen : true,
		draggable : true,
		resizable : false,
		model : true,
		title : "jsonData"
	});

}

CAW_UI.buildTableList = function(selector, data) {
	$(selector+" table").remove();
	$("#working-area").append($("<table id='response-table'>"));
	for ( var i = 0; i < data.tables.length; i++) {
		row = $("<tr class='ui-widget ui-table-header'>");
		
		row.append($('<td>').append('<a href=table_rows.html?table_name='+data.tables[i].name+'>'+data.tables[i].name+'</a>'));
		
		row.append($('<td>').append('<img src="images/edit.jpg"/>'));
		
		$("#response-table").append(row);
	}
}

CAW_UI.plotRowListResultTable = function(jsonRowList) {
	$("#working-area table").remove();

	$("#working-area").append($("<table id='response-table'>"));
	$("#response-table").append($("<tr id='families' class='ui-widget ui-table-header'>").append($('<td>').text('ROW KEY')));

	if (jsonRowList != null && jsonRowList.familyNames != null) {

		var familyNames = jsonRowList.familyNames;

		// header column families
		for ( var i = 0; i < familyNames.length; i++) {
			$("#families").append($('<td>').text(familyNames[i]).append($("<img src='images/hide.jpg'/>")));
			
		}

		if (jsonRowList.rows != null) {
			for ( var row = 0; row < jsonRowList.rows.length; row++) {
				rowkey = jsonRowList.rows[row].rowkey;

				rowkeyCssClazz = null;
				if (row % 2 == 0) {
					rowkeyCssClazz = "ui-table-even";
				} else {
					rowkeyCssClazz = "ui-table-odd";
				}
				tableRow = $("<tr id='" + rowkey + "' class='" + rowkeyCssClazz + "'>");
				tableRow.append($("<td class='ui-widget ui-row-key'>").text(rowkey).append($("<img src='images/versions.jpg'/>")));
				
				
				for ( var column = 0; column < familyNames.length; column++) {
					columnFamilyTable = $("<table>");
					families = jsonRowList.rows[row].family;
					for ( var i = 0; i < families.length; i++) {
						if (familyNames[column] == families[i].name) {
							for ( var k = 0; k < families[i].qualifier.length; k++) {
								columnFamilyTable.append($("<tr>")
										.append($("<td class='ui-widget ui-qualifier-name'>").text(families[i].qualifier[k].name))
										.append($("<td class='ui-widget ui-qualifier-value'>").text(families[i].qualifier[k].value)));
							}
						}
					}
					tableRow.append($("<td class='ui-column-family'>").append(columnFamilyTable));
				}
				$("#response-table").append(tableRow);
			}
		}
	}
}

CAW_UI.errorDialog = function showWSErrorDialog(XMLHttpRequest, textStatus, errorThrown) {
	try {
		alert(JSON.stringify(XMLHttpRequest) + "\n" + textStatus + "\n" + errorThrown);
	} catch (ex) {
	} finally {
	}
}

/**
 * Init get by rowkey dialog with autocomplete on tablename
 */
CAW_UI.buildRowKeyGetDialog = function(left, top, tableDescriptors, onClickFunction) {

	// construct get dialog
	$("#get_dialog").dialog({
		autoOpen : false,
		draggable : false,
		resizable : false,
		position : [ left, top ]
	});

	// construct buttons
	$("#get_dialog_cancel").button();
	$("#get_dialog_cancel").click(function() {
		$("#get_dialog").dialog("close");
	});

	$("#get_dialog_execute").button();
	$("#get_dialog_execute").click(function() {
		$("#get_dialog").dialog("close");
		var getModel = new Object();
		getModel.tableName = $("#get_dialog_table_name").attr('value');
		getModel.versions = $("#get_dialog_additional_fields_versions").attr('value');
		getModel.timestamp = $("#get_dialog_additional_fields_timestamp").attr('value');

		var rowkeys = $("#get_dialog").find(".formFieldDynamicRowKey");
		if (rowkeys != null) {
			getModel.rowkeys = new Array();
			var j = 0;
			for ( var i = 0; i < rowkeys.length; i++) {
				if (rowkeys[i].value != null && rowkeys[i].value != "") {
					getModel.rowkeys[j] = rowkeys[i].value;
					j++;
				}
			}
		}

		var columns = $("#get_dialog").find(".formFieldDynamicColumns");
		if (columns != null) {
			getModel.columns = new Array();
			j = 0;
			for ( var i = 0; i < columns.length; i++) {
				if (columns[i].value != null && columns[i].value != "") {
					getModel.columns[j] = columns[i].value;
					j++;
				}
			}
		}

		onClickFunction(getModel);
	});

	// dynamic adding of additional rowkeys
	$("#get_dialog_row_key_plus").click(function() {
		var rowKeyTr = $(this).parent().parent();
		var newRowKeyTr = rowKeyTr.clone();
		plusImage = newRowKeyTr.find("#get_dialog_row_key_plus");
		plusImage.remove();
		rowKeyTr.before(newRowKeyTr);
	});

	// dynamic adding of additional columns
	$("#get_dialog_additional_fields_columns_plus").click(function() {
		var rowKeyTr = $(this).parent().parent();
		var newRowKeyTr = rowKeyTr.clone();
		plusImage = newRowKeyTr.find("#get_dialog_additional_fields_columns_plus");
		plusImage.remove();
		rowKeyTr.before(newRowKeyTr);
	});

	// hide title of dialog
	$(".ui-dialog-titlebar").hide();

	// additional fields dialog
	$("#get_dialog_additional_fields").hide();
	$("#get_dialog_additional_fields_header").click(function() {
		$("#get_dialog_additional_fields").slideToggle(10);
	});

	// convert table descriptor to list of names for auto-complete
	var tableNames = new Array();
	if (tableDescriptors != null && tableDescriptors.tables != null) {
		for ( var i = 0; i < tableDescriptors.tables.length; i++) {
			tableNames[i] = tableDescriptors.tables[i].name
		}
	}

	// init auto-complete
	$("#get_dialog_table_name").autocomplete({
		minLength : 0,
		delay : 0
	});

	// register on click event for auto-complete
	$("#get_dialog_table_name").click(function() {
		if ($(this).autocomplete("widget").is(":visible")) {
			$(this).autocomplete("close");
			return false;
		}

		$(this).autocomplete({
			source : tableNames
		});

		$(this).autocomplete("search", "");
		$(this).focus();
		return false;
	});

}
